<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}

require_once('adminApp.php');

/** 系统会员 */
class UserController extends AdminAppController {
	
    public function __construct() {		
    parent::__construct();
    }

    /** 后台会员列表 */
    public function actionDefault(){

        $page = intval($_REQUEST["p"]) ? $_REQUEST["p"] : 1;
        $size = 10;

        $res = array();
        $userLogic = $this->Logic('UserLogic');

        $username = $_POST['username'];
        $realname = $_POST['realname'];
        $usertype_id = $_POST['usertype_id'];
        $verify = $_POST['verify'];
        $verify_real = $_POST['verify_real'];

        try{
            // 获取会员等级
            $userType = $userLogic->getUserTypeList("ID, TYPE_NAME", "1=1");
            if($userType["data"]){
                    $userType_arr = array();
                    foreach($userType["data"] as $k=>$v){
                            $userType_arr["a" . $v["ID"]] = $v["TYPE_NAME"];
                    }
            }
            // 获取会员列表
            $userconf = " 1=1 ";
            if($verify) $userconf.= " AND STATE_INFO=".intval($verify-1)." ";
            if($verify_real) $userconf.= " AND  STATE_REAL=".intval($verify_real-1)." ";
            if($username) $userconf.= " AND USERNAME='".$username."' ";
            if($realname) $userconf.= " AND REALNAME='".$realname."' ";
            if($usertype_id) $userconf.= " AND USERTYPE_ID='".$usertype_id."' ";
            //echo $userconf;
            $res = $userLogic->getUserInfoList("*", $userconf, "order by id desc", $page, $size);
            if($res['data']){
                    foreach($res['data'] as $k=>&$v){
                            $v["type"]["TYPE_NAME"] = $userType_arr["a" . $v["USERTYPE_ID"]]; // 会员等级
                    }
            }
            $par = array ();
            $_REQUEST["c"] = "user";
            $res['page'] = page :: listPage($par, $res["count"], $page, $size);
        }catch(Exception $e){
                echo $e->getMessage();
        }
        // 获取会员等级        
        T::$tpl->set("userType", $userType["data"]);  

        T::$tpl->set("res", $res);
    }
    
    /** 后台会员等级列表 */
    public function actionType(){
        
        $page = intval($_REQUEST["p"]) ? $_REQUEST["p"] : 1;
        $size = 10;
        
        $res = array();
        $userLogic = $this->Logic('UserLogic');
        try{
            $res = $userLogic->getUserTypeList("*", "1=1", "order by id desc", $page, $size);
            $par = array ();
            $_REQUEST["c"] = "user";
            $_REQUEST["a"] = "type";
            $res['page'] = page :: listPage($par, $res["count"], $page, $size);
        }catch(Exception $e){
            echo $e->getMessage();
        }
        
        T::$tpl->set("res", $res);
    }
    
    /** 添加会员 */
    public function actionAdd(){ 
        T::$tplFile = "admin/user/act";
        $userLogic = $this->Logic('UserLogic');

        if($_POST['actname']=='save'){
            $user_data = $_POST;
            if($user_data){

                foreach($user_data as $k=>$v)
                {
                        $user_data[$k] = htmlspecialchars(addslashes($v));
                }

                if(!preg_match('/^(?:13\d{9}|15[0|1|2|3|5|6|7|8|9]\d{8}|18[0|2|3|5|6|7|8|9]\d{8}|14[5|7]\d{8})$/',$user_data['username'])){
                        $this->sysMessage("手机号格式不对");
                } 
                $cond = "USERNAME ='".$user_data['username']."'";
                $uItem = $userLogic->getUserItem('*', $cond);
                if($uItem){
                        return $this->sysMessage("手机号格式已注册过!");
                }

                if(trim($user_data['password'])!=trim($user_data['password_confirm']))
                {
                        return  $this->sysMessage("两次密码不一样");
                }
                if(trim($user_data['password'])=='')
                {
                        return $this->sysMessage("密码不能为空");
                     
                }
                    
               $this->SaveData($_POST, 'add');        
            }else{
                return $this->sysMessage("信息不完整");
            }
                       
        } 
     
        // 获取会员等级        
        $userType = $userLogic->getUserTypeList("ID, TYPE_NAME", "1=1");
        T::$tpl->set("userType", $userType["data"]);    

    }
    
    /** 添加用户类型 */
    public function actionType_Add(){
        if($_POST['actname']=='save'){
            $type_data = $_POST;
            if($type_data){
                foreach($type_data as $k=>$v)
                {
                        $type_data[$k] = htmlspecialchars(addslashes($v));
                }

                if(isset($type_data["id"]) && $type_data["id"] != ""){
                    $this->TypeSaveData($type_data, "edit");
                }else{
                    $this->TypeSaveData($type_data, "add");
                }

            }else{
                    return $this->sysMessage("信息不完整");
            }
            
            return;
        }       

        $userLogic = $this->Logic('UserLogic');
        $id = intval($_REQUEST["id"]);        
        
        if($id){
            // 获取会员类型
            $type = $userLogic->getUserTypeItem("id, type_name, monthly_rate", "ID={$id}");            
            T::$tpl->set("type", $type);
        }
    }
    
    /** 修改会员 */
    public function actionEdit(){
        T::$tplFile = "admin/user/act";         
        $id = $_REQUEST['id'];
        if($id){            
            if($_POST['actname']=='save'){
                $this->SaveData($_POST, 'edit');
            }else{
                 $userLogic = $this->Logic('UserLogic');
                // 获取会员等级        
                $userType = $userLogic->getUserTypeList("ID, TYPE_NAME", "1=1");
                T::$tpl->set("userType", $userType["data"]);

                // 获取会员信息
                $user = $userLogic->getUserItem("*", "ID={$id}");
                T::$tpl->set("user", $user);

            }
        }else{
            $this->sysMessage("修改失败,要修改的ID异常");
        }       
    }   

    /** 修改会员资料 */
    public function actionEditInfo(){
        T::$tplFile = "admin/user/actinfo";         
        $id = $_REQUEST['id'];
        if($id){            
            if($_POST['actname']=='save'){
                $this->SaveData($_POST, 'editinfo');
            }else{
                //显示
                $user = $this->Logic('UserLogic')->getUserItem("*", "ID={$id}");
                $user['info'] = $this->Logic('UserLogic')->getUserInfo("*","ID=".$user['INFOID']);
                $user['info']['RELATIVE'] = $this->string2array($user['info']['RELATIVE']); 
                $user['info']['WORK'] = $this->string2array($user['info']['WORK']); 
                $user['info']['ASSET'] = $this->string2array($user['info']['ASSET']); 
                //print_r($user);
                T::$tpl->set('user', $user);
            }
        }else{
            $this->sysMessage("修改失败,要修改的ID异常");
        }       
    }   
    
    /** 保存添加、修改 */
    private function SaveData($data, $type){
        $userLogic = new UserLogic();
        if($type=='add'){                
            $data['password'] =  md5($data['password']);
            $uid = $userLogic->addUserItem($data);

            $datainfo['userid'] = $uid;
            $infoid = $userLogic->addUserInfo($datainfo);

            $dataArr['INFOID'] = $infoid;
            $userLogic->updateFieldVal($dataArr, "ID=".$uid);
                        
            if($uid && $infoid){
                $this->sysMessage("添加成功");
            }else{
                $this->sysMessage("添加失败,请重试");
            }
        }elseif($type=='edit'){
            if($data['password']) $newdata['password'] = md5($data['password']);
            $newdata['STATE'] = $data['state'];
            $newdata['usertype_id'] = $data['usertype_id'];
            $uid = $userLogic->updateFieldVal($newdata, "ID={$data["id"]}");
            if($uid){
                $this->sysMessage("修改成功");
            }else{
                $this->sysMessage("修改失败,请重试");
            }
        }elseif($type=='editinfo'){
            $dataArr = array();
            $dataArr['STATE_REAL'] = RequestUtil::getPost('STATE_REAL');
            $dataArr['STATE'] = RequestUtil::getPost('STATE');

            $dataArr['REALNAME'] = RequestUtil::getPost('REALNAME');
            $dataArr['IDCARD'] = RequestUtil::getPost('IDCARD');
            $dataArr['GEO_CODE'] = RequestUtil::getPost('geo_code');
            $dataArr['ADDRESS'] = RequestUtil::getPost('ADDRESS');
            $dataArr['SEX'] = RequestUtil::getPost('SEX');
            $dataArr['EMAIL'] = RequestUtil::getPost('EMAIL');
            $dataArr['QQ'] = RequestUtil::getPost('QQ');
            $dataArr['EDUCATION'] = RequestUtil::getPost('EDUCATION');
            $dataArr['BIRTHDAY'] = RequestUtil::getPost('BIRTHDAY');
            $dataArr['MARRY'] = RequestUtil::getPost('MARRY');
            $dataArr['HASCHILD'] = RequestUtil::getPost('HASCHILD');

            $RELATIVE = array();
            $RELATIVE['NAME'] = RequestUtil::getPost('RELATIVE_NAME');
            $RELATIVE['SHIP'] = RequestUtil::getPost('RELATIVE_SHIP');
            $RELATIVE['TEL'] = RequestUtil::getPost('RELATIVE_TEL');
            $dataArr['RELATIVE'] = $this->array2string($RELATIVE,0);

            $WORK = array();
            $WORK['NAME'] = RequestUtil::getPost('WORK_NAME');
            $WORK['TYPE'] = RequestUtil::getPost('WORK_TYPE');
            $WORK['SIZE'] = RequestUtil::getPost('WORK_SIZE');
            $WORK['STATE'] = RequestUtil::getPost('WORK_STATE');
            $WORK['JOB'] = RequestUtil::getPost('WORK_JOB');
            $WORK['MONEY'] = RequestUtil::getPost('WORK_MONEY');
            $dataArr['WORK'] = $this->array2string($WORK);

            $ASSET = array();
            $ASSET['HOUSE'] = RequestUtil::getPost('HOUSE');
            $ASSET['HOUSE_DEAL'] = RequestUtil::getPost('HOUSE_DEAL');
            $ASSET['CAR'] = RequestUtil::getPost('CAR');
            $ASSET['CAR_DEAL'] =RequestUtil::getPost('CAR_DEAL');
            $ASSET['CAR_ID'] = RequestUtil::getPost('CAR_ID');
            $dataArr['ASSET'] = $this->array2string($ASSET);
            //print_r($dataArr);
            $upinfo = $userLogic->updateInfoVal($dataArr, "ID=".$data["id"]);
            if($upinfo){
                $this->sysMessage("修改成功");
            }else{
                $this->sysMessage("修改失败,请重试");
            }
        }else{
            $this->sysMessage("未知操作..");
        }
    }   
    
    /** 会员等级保存添加、修改 */
    private function TypeSaveData($data, $type){
        $userLogic = new UserLogic();
        
        // 条件过滤
        if(!isset($data["type_name"]) || empty($data["type_name"])){
            $this->sysMessage("添加失败,请重试");
        }
        
        if($type=='add'){                
            
            $res = $userLogic->addUserType($data);
                        
            if($res){
                $this->sysMessage("添加成功");
            }else{
                $this->sysMessage("添加失败,请重试");
            }
        }elseif($type=='edit'){
            
            $res = $userLogic->updateUserType($data, "ID={$data["id"]}");

            if($res){
                $this->sysMessage("修改成功");
            }else{
                $this->sysMessage("修改失败,请重试");
            }
        }else{
            $this->sysMessage("未知操作..");
        }
    }   
    
    /** 删除会员 */
    public function actionDel(){
       T::$tplFile='public/empty';
        $id = intval($_REQUEST['id']);
        if($id){
                $userLogic = new UserLogic();
                
                $user = $userLogic->getUserItem("money, money_frozen", "ID={$id}");
                if((float)$user["MONEY"] != 0.00 || (float)$user["MONEY_FROZEN"] != 0.00){
                    return $this->sysMessage("删除失败，该会员资金项不为0.00",array("返回菜单页"=>"admin.php?ctl=user"), 3);
                }

                $userLogic->delUserInfo("USERID={$id}");
                $userLogic->delUser("ID={$id}");                
                return $this->sysMessage("操作成功",array("返回菜单页"=>"admin.php?ctl=user"), 3);
        }else{
                $this->sysMessage("删除失败,ID异常");
        }
    }   
    
    /** 删除会员等级 */
    public function actionType_Del(){
       T::$tplFile='public/empty';
        $id = intval($_REQUEST['id']);
        if($id){
                $userLogic = new UserLogic();
                
                $countUser = $userLogic->getCountUser("USERTYPE_ID={$id}");
                if($countUser > 0){
                    return $this->sysMessage("删除失败, 该等级包含会员",array("返回菜单页"=>"admin.php?ctl=user&act=type"), 3);
                }

                $userLogic->delUserType("ID={$id}");
                return $this->sysMessage("操作成功",array("返回菜单页"=>"admin.php?ctl=user&act=type"), 3);
        }else{
                $this->sysMessage("删除失败,ID异常");
        }
    }  
 
}//End CLass
?>